package cn.adelyn.blog.file.controller;

import cn.adelyn.base.secureweb.AuthUserContext;
import cn.adelyn.blog.file.pojo.dto.InsertShareInfoDTO;
import cn.adelyn.blog.file.service.ShareService;
import cn.adelyn.common.core.response.ServerResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;

@Validated
@RestController
public class ShareController {

    @Autowired
    ShareService shareService;

    @GetMapping("/{shareId}")
    public ServerResponseEntity<String> getSharedFileDownloadUrl(@PathVariable("shareId") @NotNull Long shareId) {
        return ServerResponseEntity.success(shareService.genSharedFileDownloadUrl(shareId));
    }

    @PostMapping("/shareInfo")
    public ServerResponseEntity insertShareInfo(@Valid InsertShareInfoDTO insertShareInfoDTO) {
        insertShareInfoDTO.setUserId(AuthUserContext.get().getUserId());
        shareService.insertShareInfo(insertShareInfoDTO);
        return ServerResponseEntity.success();
    }

    @DeleteMapping("/shareInfo")
    public ServerResponseEntity deleteShareInfo(@NotNull(message = "shareId 不能为空~") Long shareId) {
        shareService.deleteShareInfo(shareId);
        return ServerResponseEntity.success();
    }
}
